Skip to content

Conversation

@VaggelisD
Copy link
Collaborator

@VaggelisD VaggelisD commented Dec 9, 2025

Follow up of #6354

BigQuery

BigQuery's DATE_DIFF has the following semantics:
1.WEEK is Sunday-based while ISOWEEK is Monday-based; One can also shift to any day through WEEK(<day>)
2. Date part boundaries are respected i.e crossings are counted for

This explains the following result:

# WEEK diff between Saturday and Sunday, counts as 1 because of WEEK (Sunday) crossing 
bq> SELECT DATE_DIFF('2026-01-04', '2026-01-03', WEEK);
1

# ISOWEEK diff counts as 0 because of ISOWEEK (Monday) is not crossed 
bq> SELECT DATE_DIFF('2026-01-04', '2026-01-03', WEEK);
0

DuckDB

In contrast, in DuckDB:

  1. WEEK is Monday based
  2. Crossings are not respected
# Monday crossing does NOT count: 
D SELECT DATE_DIFF('WEEK', '2026-01-03'::DATE, '2026-01-06'::DATE) AS num_weeks;
┌───────────┐
│ num_weeks │
│   int64   │
├───────────┤
│     0     │
└───────────┘

# Count increases only when day delta exceeds >= 7:
D SELECT DATE_DIFF('WEEK', '2026-01-03'::DATE, '2026-01-10'::DATE) AS num_weeks;
┌───────────┐
│ num_weeks │
│   int64   │
├───────────┤
│     1     │
└───────────┘

Solution

This PR attempts to match the semantics by:
a. [BigQuery] Canonicalizing WEEK to WEEK(Sunday) at parse time
b. [BigQuery] Adding a new arg in exp.DateDiff for "date part boundary" semantics
c. [DuckDB] Shifting the week start through DATE_TRUNC to account for (b)

Docs

BQ DATE_DIFF | DuckDB DATE_DIFF

@VaggelisD VaggelisD merged commit 8b5298a into main Dec 9, 2025
8 checks passed
@VaggelisD VaggelisD deleted the vaggelisd/bq_datediff_ddb branch December 9, 2025 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants